{% extends 'base.html' %}
{% load mytags %}
{% block content %}
{% include 'nav_cat_bar.html' %}

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-10">
            <div class="ibox float-e-margins">

                <div class="ibox-title">
                    <h5> 查看发布任务</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>

                <div class="ibox-content">
                    <form id="asset_form">
                        <div class="col-sm-7" style="padding-left: 0px">
                            <label>
                                <select name="env" class="form-control m-b input-sm" onchange="change_info()">
                                    <option value="">环境类型</option>
                                    {% for env in env_all %}
                                        {% if env|list_0|int2str == env_value|int2str %}
                                            <option value="{{ env|list_0 }}" selected> {{ env|list_1|slice:":100" }}</option>
                                        {% else %}
                                            <option value="{{ env|list_0 }}"> {{ env|list_1|slice:":100" }}</option>
                                        {% endif %}
                                    {% endfor %}
                                </select>
                            </label>

                            <label>
                                <select name="status" class="form-control m-b input-sm" onchange="change_info()">
                                    <option value="">状态</option>
                                    {% for status in status_all %}
                                        {% if status|list_0|int2str == status_value|int2str %}
                                            <option value="{{ status|list_0 }}" selected> {{ status|list_1|slice:":20" }}</option>
                                        {% else %}
                                            <option value="{{ status|list_0 }}"> {{ status|list_1|slice:":20" }}</option>
                                        {% endif %}
                                    {% endfor %}
                                </select>
                            </label>
                        </div>

                        <div class="col-sm-4" style="padding-right: 0">
                             <div class="input-group inline-group">
                                <input type="text" class="form-control m-b input-sm" id="search_input" name="keyword" value="{{ keyword }}" placeholder="Search">
                                <input type="text" style="display: none">
                                <div class="input-group-btn">
                                    <button id='search_btn' href="{% url 'publish_task_list' %}?search=true" type="button" class="btn btn-sm btn-primary search-btn"  onclick="change_info()">
                                        - 搜索 -
                                    </button>
                                </div>
                            </div>
                        </div>
                        <table class="table table-striped table-bordered table-hover " id="editable" >
                            <thead>
                                <tr>
                                    <th class="text-center">发布序列号</th>
                                    <th class="text-center">生产线</th>
                                    <th class="text-center">产品名称</th>
                                    <th class="text-center">环境类型</th>
                                    <th class="text-center">版本</th>
                                    <th class="text-center">提交人</th>
                                    <th class="text-center">项目负责人</th>
                                    <th class="text-center">计划发版时间</th>
                                    <th class="text-center">状态</th>
                                    <th class="text-center">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for task in publish_tasks.object_list %}
                                <tr class="gradeX">
                                    <td class="text-center">
                                        <a href="{% url 'publish_task_detail' %}?id={{ task.id }}"> {{ task.seq_no }} </a>
                                    </td>
                                    <td class="text-center">
                                        {{ task.product|get_product_name }}
                                    </td>
                                    <td class="text-center">
                                        {{ task.project }}
                                    </td>
                                    <td class="text-center">
                                        {{ task.env|get_env_name }}
                                    </td>
                                    <td class="text-center">
                                        {{ task.version }}
                                    </td>
                                    <td class="text-center">
                                        {{ task.submit_by }}
                                    </td>

                                    <td class="text-center">
                                        {{ task.owner }}
                                    </td>
                                    <td class="text-center">
                                        {{ task.publish_time }}
                                    </td>
                                    <td class="text-center">
                                        {{ task.status|get_status_name }}
                                    </td>

                                    <td class="text-center">
                                        {% if perms.account.perm_can_cancel_publish_task %}
                                            <a value="{{ task.id }}" class="btn btn-xs btn-warning btn-cancel {% if task.status != '3' %} disabled {% endif %}">作废</a>
                                        {% endif %}
                                        {% if perms.account.perm_can_trash_publish_task %}
                                            <a value="{{ task.id }}" class="btn btn-xs btn-warning btn-trash {% if task.status != '3' %} disabled {% endif %}">驳回</a>
                                            <a value="{{ task.id }}" class="btn btn-xs btn-warning btn-reapply {% if task.status != '3' %} disabled {% endif %}">重审</a>
                                            <a value="{{ task.id }}" class="btn btn-xs btn-warning btn-resubmit {% if task.status != '3' %} disabled {% endif %}">重提</a>&nbsp;&nbsp;
                                        {% endif %}
                                        {% if perms.account.perm_can_deploy_publish_task %}
                                            <a value="{{ task.id }}" class="btn btn-xs btn-danger btn-deploy {% if task.status != '3' %} disabled {% endif %}">发布</a>
                                        {% endif %}
                                        {% if perms.account.perm_can_rollback_publish_task %}
                                            <a value="{{ task.id }}" class="btn btn-xs btn-danger btn-rollback {% if task.status != '4' %} disabled {% endif %}">回滚</a>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="dataTables_info" id="editable_info" role="status" aria-live="polite">
                                    Showing {{ publish_tasks.start_index }} to {{ publish_tasks.end_index }} of {{ p.count }} entries
                                </div>
                            </div>
                            {% include 'paginator.html' %}
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block self_footer_js %}
    <script>
    $(document).ready(function() {
        $('.btn-trash').click(function(){
            var url = '{% url 'publish_task_trash' %}?id=' + $(this).attr('value');
            if (confirm("确定驳回?")) {
                $.ajax({
                    type: 'GET',
                    url: url,
                    data: {},
                    success: function (data) {
                        if (data == 'error' || data == '' || data == undefined) {
                            layer.alert('提交出错，联系管理员！')
                        }
                        parent.location.reload();
                    }
                });
            }
        });

        $('.btn-reapply').click(function(){
            var url = '{% url 'publish_task_reapply' %}?id=' + $(this).attr('value');
            if (confirm("确定重审?")){
                $.ajax({
                    type: 'GET',
                    url: url,
                    data: {},
                    success: function(data){
                        if (data == 'error' || data == '' || data == undefined){
                            layer.alert('提交出错，联系管理员！')
                        }
                        parent.location.reload();
                    }
                });
            }
        });

        $('.btn-resubmit').click(function(){
            var url = '{% url 'publish_task_resubmit' %}?id=' + $(this).attr('value');
            if (confirm("确定重提?")){
                $.ajax({
                    type: 'GET',
                    url: url,
                    data: {},
                    success: function(data){
                        if (data == 'error' || data == '' || data == undefined){
                            layer.alert('提交出错，联系管理员！')
                        }
                        parent.location.reload();
                    }
                })
            }
        });

        $('.btn-cancel').click(function(){
            var url = '{% url 'publish_task_cancel' %}?id=' + $(this).attr('value');
            if (confirm("确定作废?")){
                $.ajax({
                    type: 'GET',
                    url: url,
                    data: {},
                    success: function(data){
                        if (data == 'error' || data == '' || data == undefined){
                            layer.alert('提交出错，联系管理员！')
                        }
                        parent.location.reload();
                    }
                })
            }
        });


        $('.btn-deploy').click(function(){
            if (confirm("确定发布?")) {
                var task_id = $(this).attr('value');
                var url1 = '{% url 'publish_task_deploy_judge' %}?task_id=' + task_id;
                $.ajax({
                    type: 'GET',
                    url: url1,
                    data: { },
                    success: function (data1){
                        if(data1 == 'true') {
                            window.location.href='{% url 'publish_task_deploy' %}?id=' + task_id;
                        }
                        else if(data1 == 'false'){
                            alert('发布中，请勿重复操作！')
                        }
                        else{
                            alert('发布报错！')
                        }
                    }
                });
            }
        });

        $('.btn-rollback').click(function(){
            var url = '{% url 'publish_task_rollback' %}?id=' + $(this).attr('value');
            if (confirm("确定回滚?")) {
                $.ajax({
                    type: 'GET',
                    url: url,
                    data: {},
                    success: function (data) {
                        if (data == 'error' || data == '' || data == undefined) {
                            layer.alert('提交出错，联系管理员！')
                        }
                        parent.location.reload();
                    }
                });
            }
        });

    });

    function change_info(){
        var args = $("#asset_form").serialize();
        window.location = "{% url 'publish_task_list' %}?" + args
    }
    </script>
{% endblock %}

